<template>
  <div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="基础信息" name="1">
        <lineBaseInfo ref="tab1" :type="type"></lineBaseInfo>
      </el-tab-pane>
      <el-tab-pane label="产品特色" name="2">
        <lineFeature ref="tab2" :type="type"></lineFeature>
      </el-tab-pane>
      <el-tab-pane label="行程信息" name="3">
        <lineJourney ref="tab3" :type="type"></lineJourney>
      </el-tab-pane>
      <el-tab-pane label="费用说明" name="4">
        <lineFee ref="tab4" :type="type"></lineFee>
      </el-tab-pane>
      <el-tab-pane label="预定须知" name="5">
        <lineBooking ref="tab5" :type="type"></lineBooking>
      </el-tab-pane>
      <el-tab-pane label="库存" name="6">
        <lineRepertory ref="tab6" :type="type"></lineRepertory>
      </el-tab-pane>
      <el-tab-pane label="产品关联" name="7">
        <lineRelevance ref="tab7" :type="type"></lineRelevance>
      </el-tab-pane>
    </el-tabs>
    <div class="mybot" v-if="type != 'look'">
      <el-button type="primary" @click="getAllData(false)">暂存</el-button>
      <el-button type="primary" @click="getAllData(true)">提交审核</el-button>
    </div>
  </div>
</template>

<script>
import lineBaseInfo from "./line-baseInfo";
import lineFeature from "./line-feature";
import lineJourney from "./line-journey";
import lineFee from "./line-fee";
import lineBooking from "./line-booking";
import lineRepertory from "./line-repertory";
import lineRelevance from "./line-relevance";
import {
  addOrModifyCommodity,
  getCommodityDetail
} from "api/lineManagement/lineCommodity";
import {deepClone} from 'common/js/util';
export default {
  components: {
    lineBaseInfo,
    lineFeature,
    lineJourney,
    lineFee,
    lineBooking,
    lineRepertory,
    lineRelevance
  },
  data() {
    return {
      activeName: "1",
      type: "add"
    };
  },
  methods: {
    handleClick() {
      if(this.activeName == '6'){
        let obj = this.$refs.tab1.getChengben()
        this.$refs.tab6.setChengben(obj)
      }
    },
    // 获取详情
    async _getCommodityDetail() {
      let res = await getCommodityDetail({ commodity_id: this.curid });
      let arr = this.getComponents();
      let data = res.rData[0];
      this.changeFen(data, "shrink");
      arr.forEach(item => {
        item.setData(data);
      });
    },
    // getC
    getComponents() {
      let arr = [];
      for (let i = 1; i <= 7; i++) {
        arr.push(this.$refs["tab" + i]);
      }
      return arr;
    },
    getAllData(type) {
      let arr = this.getComponents();
      // type暂时不要  来判断是否需要校验
      let parr = arr.map(item => item.getData(true));
      Promise.all(parr)
        .then(res => {
          let obj = res.reduce((pre, cur) => {
            let myobj = { ...pre, ...cur };
            return myobj;
          }, {});
          // 转化
          let newObj = deepClone(obj);
          // 如果是保存暂存的话  0暂存 1提交审核
          newObj.is_submit = type ? 1 : 0

          this.changeFen(newObj, "magnify");
          console.log(obj, "转换后的数据");
          // 发送  添加
          return addOrModifyCommodity(newObj);
        })
        .then(() => {
          // 添加成功
          this.$message({
            message: "添加成功",
            type: "success"
          });
          this.$router.push("/home/lineCommodity");
        }, () => {
          this.$message.error('请将各个模块必填项目填写完整')
        });
    },
    changeFen(obj, type) {
      console.log('zhuanhuan de duix', obj)
      let changeFn;
      if (type == "magnify") {
        changeFn = val => val * 100 || 0;
      } else if (type == "shrink") {
        changeFn = val => val / 100 || 0;
      }
      // 煮熟费
      obj.hotel_detail && obj.hotel_detail.forEach(item => {
        item.hotel_price = changeFn(item.hotel_price);
      });
      obj.append_content && obj.append_content.forEach(item => {
        item.dp_price = changeFn(item.dp_price);
      });
      obj.pricing = changeFn(obj.pricing);
      obj.cb_price = changeFn(obj.cb_price);
      obj.child_cb_price = changeFn(obj.child_cb_price);
      obj.jt_sprice = changeFn(obj.jt_sprice);
      if (obj.fj_price) {
        obj.fj_price = changeFn(obj.fj_price);
      }
      if (obj.dc_price) {
        obj.dc_price = changeFn(obj.dc_price);
      }
      if (obj.qc_price) {
        obj.qc_price = changeFn(obj.qc_price);
      }
      obj.zs_sprice = changeFn(obj.zs_sprice);
      obj.hotel_unit_price = changeFn(obj.hotel_unit_price);
      obj.hotel_standard = changeFn(obj.hotel_standard);
      obj.jd_sprice = changeFn(obj.jd_sprice);
      obj.jd_content.forEach(item => {
        item.jd_price = changeFn(item.jd_price);
      });
      obj.sp_sprice = changeFn(obj.sp_sprice);
      if (obj.zc_price) {
        obj.zc_price = changeFn(obj.zc_price);
      }
      if (obj.wc_price) {
        obj.wc_price = changeFn(obj.wc_price);
      }
      if (obj.other_sprice) {
        obj.other_sprice = changeFn(obj.other_sprice);
        obj.other_content.forEach(item => {
          item.other_price = changeFn(item.other_price);
        });
      }
      obj.repertoryArr.forEach(item => {
        item.month_detail.forEach(subitem => {
          subitem.price = changeFn(subitem.price);
          subitem.child_price = changeFn(subitem.child_price);
          subitem.diff_price = changeFn(subitem.diff_price);
        });
      });
    }
  },
  created() {
    this.type = this.$route.query.type;
    if (this.type) {
      // 获取详情
      this.curid = this.$route.query.id;
      this._getCommodityDetail();
    }
  }
};
</script>

<style lang="scss" scoped>
.mybot {
  display: flex;
  justify-content: flex-end;
  padding-right: 50px;
  padding-top: 20px;
}
</style>
